import 'dart:collection';

import 'package:flutter/material.dart';
import 'package:whale/pages/means/subs/means_detail/model/doc_noti_model.dart';
import 'package:whale/pages/means/subs/means_detail/model/model.dart';
import 'package:whale/helper/utils/util.dart';
import 'widgets/widgets.dart';

import 'index.dart';

class MeansDetailController extends GetxController {
  MeansDetailController();

  final state = MeansDetailState();

  void actionTapTag(String id) {
    if (isZeroList(state.mod?.problemList)) {
      return;
    }
    for (var i = 0; i < state.mod!.problemList!.length; i++) {
      var e = state.mod!.problemList![i];
      if (e.id == id) {
        showTagAlert(e, iden: (i + 1).toString());
      }
    }
  }

  String? getNoteId(LinkedHashMap<Object, String>? attributes) {
    var attr = attributes;
    var id = attr?['data-highlight-id'].toString();
    return id;
  }

  int getNoteIndex(LinkedHashMap<Object, String>? attributes) {
    var id = getNoteId(attributes);
    var idx = 0;
    if (isZeroList(state.mod?.problemList)) {
      return 0;
    }
    for (var i = 0; i < state.mod!.problemList!.length; i++) {
      var e = state.mod!.problemList![i];
      if (e.id == id) {
        idx = i;
      }
    }
    return idx + 1;
  }

  /// 在 widget 内存中分配后立即调用。
  @override
  void onInit() {
    super.onInit();
    state.meansMd = Get.arguments;
    state.sc = ScrollController()
      ..addListener(() {
        state.offy = state.sc.offset;
      });
    state.tabCtrl = TPTabBarController()
      ..titles.value = ['正文', '审查报告', '更多']
      ..keys = [1, 2, 3].map((e) => GlobalKey()).toList()
      ..index.listen((p0) {
        if (p0 == 1 && state.notiMd == null) {
          requestNoti();
        }
      });

    getDocDetail();
  }

  void getDocDetail() async {
    var res = await MeansApi.postDocDetail(
        id: state.meansMd?.id ?? '', type: state.meansMd?.reportType ?? '');
    if (res.isSuccess) {
      var mod = TPTextModel.fromJson(res.data);
      state.mod = mod;
      if (state.meansMd?.reportType == '政策措施') {
        state.titles = [
          {
            '地域': getStr(mod.area),
          },
          {
            '索引号': getStr(mod.indexNumber),
          },
        ];
        state.titles1 = [
          [
            {
              '发布机构': getStr(mod.companyName),
            },
            {
              '关键词': getStr(mod.documentTag),
            },
            {
              '发布日期': getStr(mod.publishTime),
            }
          ],
          [
            {
              '文号': getStr(state.mod?.documentNum),
            },
            {
              '领域': getStr(state.mod?.industryType),
            },
            {
              '体裁': getStr(state.mod?.genre),
            }
          ],
        ];
      } else if (state.meansMd?.reportType == '招标采购') {
        state.titles = [
          {
            '地域': getStr(mod.area),
          },
          {
            '项目名称': getStr(mod.projectName),
          },
        ];
        state.titles1 = [
          [
            {
              '采购单位': getStr(mod.purchaseAgency),
            },
            {
              '联系人': getStr(mod.concats),
            },
            {
              '联系电话': getStr(state.mod?.concatsPhone),
            },
          ],
          [
            {
              '发布日期': getStr(state.mod?.publishTime),
            },
            {
              '领域': getStr(state.mod?.industryType),
            },
            {
              '采购代理机构': getStr(state.mod?.purchaseAgency),
            },
          ],
        ];
      } else if (state.meansMd?.reportType == '审查制度') {
        state.titles = [
          {
            '地域': getStr(mod.area),
          },
          {
            '索引号': getStr(mod.indexNumber),
          },
          {
            '发布机构': getStr(mod.companyName),
          },
          {
            '关键词': getStr(mod.documentTag),
          },
        ];
        state.titles1 = [
          [
            {
              '文号': getStr(state.mod?.documentNum),
            },
            {
              '领域': getStr(state.mod?.industryType),
            },
            {
              '体裁': getStr(state.mod?.genre),
            }
          ],
        ];
      }

      update();
    }
  }

  String getStr(String? s) {
    if (isNilStr(s)) {
      return '-';
    }
    return s!;
  }

  void requestNoti() async {
    var res = await MeansApi.postDocNoti(id: state.meansMd?.id ?? '');
    if (res.isSuccess) {
      state.notiMd = DocNotiModel.fromJson(res.data);
      update();
    }
  }

  /// 在 onInit() 之后调用 1 帧。这是进入的理想场所
  @override
  void onReady() {
    super.onReady();
  }

  /// 在 [onDelete] 方法之前调用。
  @override
  void onClose() {
    super.onClose();
  }

  /// dispose 释放内存
  @override
  void dispose() {
    super.dispose();
  }
}
